package com.conan.spring5.dao;

import com.conan.spring5.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * @version 加油！！！
 * @Description
 * @Author 谷藤新一GYC
 * @Date 2022/10/22 - 10:39
 */
@Repository
public class BookDAOImpl implements BookDAO{

    //注入JDBCTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void addUser(User user) {
        String sql = "insert into t_user values(0,?,?,?,0)";
        jdbcTemplate.update(sql,user.getUname(),user.getPwd(),user.getEmail());
    }

    @Override
    public void updateUser(String pwd,User user) {
        jdbcTemplate.update("update t_user set pwd = ? where id = ?",pwd,user.getId());
    }

    @Override
    public void deleteUser(Integer id) {
        jdbcTemplate.update("delete from t_user where id=?",id);
    }

    @Override
    public Integer getCount() {
        return jdbcTemplate.queryForObject("select count(*) from t_user",Integer.class);
    }

    @Override
    public User getUserById(Integer id) {
        return jdbcTemplate.queryForObject("select * from t_user where id=?",new BeanPropertyRowMapper<User>(User.class),id);
    }

    @Override
    public List<User> getUserList() {

        return jdbcTemplate.query("select * from t_user",new BeanPropertyRowMapper<User>(User.class));
    }

    @Override
    public void batchAdd(List<Object[]> batchArgs) {
        jdbcTemplate.batchUpdate("insert into t_user values(0,?,?,?,?)", batchArgs);
    }

    @Override
    public void batchUpdate(List<Object[]> batchArgs) {
        jdbcTemplate.batchUpdate("update t_user set pwd = ? where id = ?",batchArgs);
    }


    @Override
    public void batchDelete(List<Object[]> batchArgs) {
        jdbcTemplate.batchUpdate("delete from t_user where id=?",batchArgs);
    }
}
   